package thread;

//单例模式
class Singletom {
    private static Singletom instance = new Singletom();
    public static Singletom getInstance() {
        return instance;
    }

    private Singletom() {};
}


class SingletonLazy {
    private static SingletonLazy instance = null;
    private static Object locker = new Object();
    public static SingletonLazy getInstance() {
        if(instance == null) {
            //如果 instance 为 null, 就说明是首次调用, 首次调用就要考虑线程安全问题, 就要加锁
            //如果是非 null, 就说明是后续调用, 就不用加锁了
            synchronized (locker) {
                if(instance == null) {
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }
    private SingletonLazy(){ };
}

public class ThreadDemo25 {
    public static void main(String[] args) {
        Singletom singletom = Singletom.getInstance();
    }
}
